
import {shop_token,oauth_logout,user_info} from "@/js_sdk/apis/user.js"
import md5 from "@/common/SDK/md5.min.js";
const namespaced = true;

/**
 * Authorization = ‘Basic ${Base64.encode(`${website.clientId}:${website.clientSecret}`
 * clientId = shsp
 * clientSecret = shsp_secret  
*/
const Authorization = 'Basic c2hzcDpzaHNwX3NlY3JldA=='

// 
const TenantId = '000000'

// lykj-auth = token_type + 空格+ access_token
const buildAuth = ({token_type, access_token}) => `${token_type} ${access_token}`

// 本地存取 lykj-auth
const AUTH_KEY = 'AUTH_KEY'
const setAuth = auth => { uni.setStorageSync(AUTH_KEY,auth) }
const auth = uni.getStorageSync(AUTH_KEY)||''

// 本地存取登入信息
const LOGIN_INFO_KEY = 'LOGIN_INFO_KEY'
const setLoginInfo = info => { uni.setStorageSync(LOGIN_INFO_KEY,info) }
const loginInfo = uni.getStorageSync(LOGIN_INFO_KEY) || ''

// 本地存取用户信息
const USER_INFO_KEY = 'USER_INFO_KEY'
const setUserInfo = info => { uni.setStorageSync(USER_INFO_KEY,info) }
const userInfo = uni.getStorageSync(USER_INFO_KEY)
const roleNmae = loginInfo && loginInfo.role_name ? loginInfo.role_name:''

const openid=uni.getStorageSync("openid")

const clear = ()=>{
	uni.removeStorageSync(AUTH_KEY)
	uni.removeStorageSync(LOGIN_INFO_KEY)
	uni.removeStorageSync(USER_INFO_KEY)
}

const state = {
	lykjAuth: auth,
	loginInfo,
	userInfo,
	roleNmae,
	openid,
}

const mutations = {
	SET_LOGIN_INFO(state, loginInfo) {
		state.loginInfo = loginInfo;
		let {role_name, token_type, access_token} = loginInfo
		state.roleNmae = role_name
		const lykjAuth = buildAuth(loginInfo)
		state.lykjAuth = lykjAuth
		setAuth(lykjAuth)
		setUserInfo(loginInfo)
	},
	SET_USER_INFO(state, userInfo) {
		state.userInfo = userInfo;
		setUserInfo(userInfo)
	},
	SET_OPEN_ID(state, openid) {
		state.openid = openid;
	},
	CLEAR(state){
		state.lykjAuth=''
		state.loginInfo=''
		state.userInfo=''
		state.roleNmae=''
		state.openid=''
		clear()
	}
}

const actions = {
	// 登录
	Login({ commit }, userInfo) {
		
	  const username = userInfo.username.trim()
	  const password = userInfo.password
	  const code = userInfo.code
	  const uuid = userInfo.uuid
	  return new Promise((resolve, reject) => {
		  shop_token({
		  	password:md5(password),
		  	tenantId:'000000',
		  	username:username,
		  }).then(res=>{
		  	if(res.error_description){
				reject(res)
		  	}else{
		  		commit('SET_LOGIN_INFO',res);
				resolve()
		  	}
		  }).catch(e=>{
			  reject(e)
		  })
	  })
	},
	// 获取用户信息
	GetInfo({ commit, state }) {
	  return new Promise((resolve, reject) => {
	    user_info().then(res => {
		  commit('SET_USER_INFO',res.data);
	      resolve(res.data)
	    }).catch(error => {
	      reject(error)
	    })
	  })
	},
	// 退出系统
	LogOut({ commit, state }) {
	  return new Promise((resolve, reject) => {
	    oauth_logout().then(() => {
	      commit('CLEAR')
	      resolve()
	    }).catch(error => {
	      reject(error)
	    })
	  })
	}
}

const getters = {
	headers(state) {
		return state.lykjAuth ? {
			Authorization,
			'Lykj-Auth': state.lykjAuth,
			'Tenant-Id': TenantId
		}:{
			Authorization,
			'Tenant-Id': TenantId
		}
	},
	hasLogin(state){
		return !!state.lykjAuth
	}
}

export default {
	namespaced,
	state,
	mutations,
	actions,
	getters
}
